$(function () {
    $("[name='zjpcd_sbgs']").jgbmtree($("[name='zjpcd_sbbm']"), null, '', '');

    $("#zjpcd_gys").qyjgzcombobox();

    $('#grid_zjpcd').datagrid({
        url:$.webPath() + "/jsgl/zjbcd/zjpcdList",
        toolbar:"#toolbar_zjpcd",
        fitColumns:true,
        singleSelect:true,
        //pagination:true,
        //pageNumber:1,
        fit:true,
        rownumbers:true,
        showFooter:true,
        striped:true,
        columns:[
            [
                {field:'GSJC', title:'公司名称', rowspan:2, align:'center', width:100},
                {field:'BMJC', title:'部门名称', rowspan:2, align:'center', width:100},
                {field:'GYS', title:'供应商', rowspan:2, align:'center', width:150},
                {field:'QCJE', title:'期初', rowspan:2, align:'center', width:100, formatter:function (value) {
                    var sl = formatJe2(value, ",");
                    return sl;
                }},
                {title:'本期产生(+)', colspan:3, width:100},
                {field:'BQFP', title:'本期分配(-)', rowspan:2, align:'center', width:100, formatter:function (value) {
                    var sl = formatJe2(value, ",");
                    return sl;
                }},
                {field:'QMYE', title:'期末余额', rowspan:2, align:'center', width:100, formatter:function (v, r, i) {
                    var color = "#0000ff;";
                    if (v && r.GSJC != '合计') {
                        if (v < 0) color = "red;";
                        return "<a href='javascript:void(0)' style='color:{0}' class='qmye' gsid='{2}' bmid='{3}' gysid = '{4}'>{1}</a>".stringFormat(color, formatSl2(v, ",", "."), r.GSID, r.BMID, r.GYSID);
                    } else {
                        return formatSl2(v, ",", ".");
                    }
                }}
            ] ,
            [
                {field:'JE2', title:'价格差', align:'center', width:100, formatter:function (value) {
                    var sl = formatJe2(value, ",");
                    return sl;
                }},
                {field:'JE3', title:'返利追溯', align:'center', width:100, formatter:function (value) {
                    var sl = formatJe2(value, ",");
                    return sl;
                }},
                {field:'JE4', title:'费用差异', align:'center', width:100, formatter:function (value) {
                    var sl = formatJe2(value, ",");
                    return sl;
                }}
            ]
        ],
        loadFilter:function (data) {
            $.each(data.rows, function () {
                this.QMYE = this.QCJE + (this.JE2 + this.JE3 + this.JE4) - this.BQFP;
            })
            return data;
        },
        onLoadSuccess:function () {
            $(this).datagrid('selectRow', 0);
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
            $(".qmye").unbind("click").bind("click", function () {
                opentabs('资金补差明细', "jsgl/zjbcd/zjbcd?sbgsId=" + $(this).attr('gsid') + "&sbbmId=" + $(this).attr('bmid') + "&gysId=" + $(this).attr('gysid'), '');
            });
            countSumZjpcd();
        }
    });

    $("#btn_zjpcd_search").click(function () {
        $('#grid_zjpcd').datagrid('load',
            {
                gysId:$("#zjpcd_gys").val(),
                sbgsId:$("#toolbar_zjpcd input[name='zjpcd_sbgs']").val(),
                sbbmId:$("#toolbar_zjpcd input[name='zjpcd_sbbm']").val()
            }
        );
    });
})


//资金补差单合计
function countSumZjpcd() {
    var sumQcje = 0;
    var sumJe2 = 0;
    var sumJe3 = 0;
    var sumJe4 = 0;
    var sumBqfp = 0;
    var sumQmye = 0;
    var data = $("#grid_zjpcd").datagrid("getData");
    for (var i = 0; i < data.rows.length; i++) {
        var qcje = parseFloat(data.rows[i]["QCJE"]);
        var je2 = parseFloat(data.rows[i]["JE2"]);
        var je3 = parseFloat(data.rows[i]["JE3"]);
        var je4 = parseFloat(data.rows[i]["JE4"]);
        var bqfp = parseFloat(data.rows[i]["BQFP"]);
        var qmye = parseFloat(data.rows[i]["QMYE"]);
        qcje = isNaN(qcje) ? 0 : qcje;
        je2 = isNaN(je2) ? 0 : je2;
        je3 = isNaN(je3) ? 0 : je3;
        je4 = isNaN(je4) ? 0 : je4;
        bqfp = isNaN(bqfp) ? 0 : bqfp;
        qmye = isNaN(qmye) ? 0 : qmye;
        sumQcje += qcje;
        sumJe2 += je2;
        sumJe3 += je3;
        sumJe4 += je4;
        sumBqfp += bqfp;
        sumQmye += qmye;
    }
    addFooter(sumQcje, sumJe2, sumJe3, sumJe4, sumBqfp, sumQmye);
}

function addFooter(sumQcje, sumJe2, sumJe3, sumJe4, sumBqfp, sumQmye) {
    var footer = null;
    footer = [
        {"GSJC":"合计", "QCJE":formatMoneyFourWei(sumQcje), "JE2":formatMoneyFourWei(sumJe2), "JE3":formatMoneyFourWei(sumJe3), "JE4":formatMoneyFourWei(sumJe4), "BQFP":formatMoneyFourWei(sumBqfp), "QMYE":formatMoneyFourWei(sumQmye)}
    ];
    if (footer != null)
        $("#grid_zjpcd").datagrid('reloadFooter', footer);
}

//在右边center区域打开菜单，新增tab
var opentabs = function (text, url, type) {
    if ($("#tabs").tabs('exists', text)) {
        $('#tabs').tabs('close', text);
        $('#tabs').tabs('add', {
            title:text,
            cache:true,
            closable:true,
            href:$.webPath() + '/' + url
        });
    } else if (type == 'bbtj') {
        $('#tabs').tabs('add', {
            title:text,
            cache:true,
            closable:true,
            content:'<iframe scrolling="no" frameborder="0" src="' + url + '" style="width:100%;height:100%;"></iframe>'
            //href: '../' + url
        });
    } else {
        $('#tabs').tabs('add', {
            title:text,
            cache:true,
            closable:true,
//                content: '<iframe scrolling="no" frameborder="0" src="../'+url+'" style="width:100%;height:100%;"></iframe>'
            href:$.webPath() + '/' + url
        });
    }
}

